<?php
namespace App\Models\UserModels;
use App\Models\ParentModel;
use DB;
/**
 * 用户监控model
 * @author puppet
 */
class Xunpan extends ParentModel{
	/**
	* 与模型关联的数据表。
	*
	* @var string
	*/
	protected $table = 'pt_partner_comment';

	/**
	* DB::直接操作的数据库表名
	* @var string
	*/
	protected $db_table = 'pt_partner_comment';

	/**
	* 主键 默认id。
	*
	* @var string
	*/
	protected $primaryKey = 'id';

	/**
	* 指定是否模型应该被戳记时间。
	*
	* @var bool
	*/
	public $timestamps = false;
	
	
	//查询可用的会员开通套餐未到时的可用的关键词
	function selectKeyAll(){
		$sql="select a.id,a.user_id,a.name,c.num2,c.start,c.end,c.meal_name from pt_keyword as a";
		$sql.=" left join pt_partner as b on b.id=a.user_id";
		$sql.=" left join pt_partner_meal as c on c.partner_id=a.user_id";
		$sql.=" where a.status=? and b.status=1 and b.type=2 and c.start<='".date('Y-m-d',time())."' and c.end>='".date('Y-m-d',time())."'";
		$re=DB::select($sql,[1]);
		return isset($re[0]['user_id']) ? $re : false;
	}
        
        
	//查询可用的会员开通套餐未到时的可用的关键词
	function selectKeyAllByDate($date){
                $date = !empty($date)?$date:date('Y-m-d',time());
		$sql="select a.id,a.user_id,a.name,c.num2,c.start,c.end,c.meal_name from pt_keyword as a";
		$sql.=" left join pt_partner as b on b.id=a.user_id";
		$sql.=" left join pt_partner_meal as c on c.partner_id=a.user_id";
		$sql.=" where a.status=? and b.status=1 and b.type=2 and c.start<='".$date."' and c.end>='".$date."'";
		$re=DB::select($sql,[1]);
		return isset($re[0]['user_id']) ? $re : false;
	}        
        
	
	//会员总共得到的询盘数
	function commentAllCount($user_id){
		$sql="select count(id) as count from pt_partner_comment where partner_id=?";
		$re=DB::select($sql,[$user_id]);
		return isset($re[0]['count']) ? $re[0]['count'] : 0;
	}
	//会员当天得到的询盘数
	function commentDayList($user_id){
		$sql="select comment_id from pt_partner_comment where partner_id=? and create_time='".date('Y-m-d',time())."'";
		$re=DB::select($sql,[$user_id]);
		return isset($re[0]) ? $re : 0;
	}
        
	//会员当天日期得到的询盘数
	function commentDayListByDate($user_id,$date){
                $date = !empty($date)?$date:date('Y-m-d',time());
		$sql="select comment_id from pt_partner_comment where partner_id=? and create_time='".$date."'";
		$re=DB::select($sql,[$user_id]);
		return isset($re[0]) ? $re : 0;
	}        
        
        
	/*//会员该关键词当天得到的询盘数
	function commentOneCount($user_id,$key_id){
		$sql="select count(id) as count from pt_partner_comment where partner_id=? and keyword_id={$key_id} and create_time='".date('Y-m-d',time())."'";
		$re=DB::select($sql,[$user_id]);
		return isset($re[0]['count']) ? $re[0]['count'] : 0;
	}*/
	//会员添加的可用的关键次总数
	function userKeyCount($user_id){
		$sql="select count(id) as count from pt_keyword where user_id={$user_id} and status=?";
		$re=DB::select($sql,[1]);
		return isset($re[0]['count']) ? $re[0]['count'] : 0;
	}
	
	//插入获得的询盘评论
	function saveComment($data){
		DB::table("pt_partner_comment")->insertGetId($data);
	}
	
	/**
	*取出指定的评论数据
	*	contentLike 内容关键词		keyName	标题、视频用户名关键词			commentIds 筛选排除外的id集合
	*/
	function newCommentAll($contentLike,$keyName,$commentIds){
		$sql="select id from pt_keyword_video_comment where status=? and (title like '%{$keyName}%' or keyword like '%{$keyName}%') and {$contentLike}";
		$sql.=" and update_time between '".date('Y-m-d',time())."' and '".date('Y-m-d',time())." 23:59:59'";
		if($commentIds){
			$sql.=" and id not in({$commentIds})";
		}
		$re=DB::select($sql,[1]);
		return isset($re[0]['id']) ? $re : false;
	}
        
	/**
	*取出指定的评论数据
	*	contentLike 内容关键词		keyName	标题、视频用户名关键词			commentIds 筛选排除外的id集合 
	*/
	function newCommentAllBydate($contentLike,$keyName,$commentIds,$date){
                $date = !empty($date)?$date:date('Y-m-d',time());
		$sql="select id from pt_keyword_video_comment where status=? and (title like '%{$keyName}%' or keyword like '%{$keyName}%') and {$contentLike}";
		$sql.=" and update_time between '".$date."' and '".$date." 23:59:59'";
		if($commentIds){
			$sql.=" and id not in({$commentIds})";
		}
		$re=DB::select($sql,[1]);
		return isset($re[0]['id']) ? $re : false;
	}        
	
	
	//指定用户得到的视频评论数
	function getCommentsum($partner_id){
		$sql="select count(id) as count from pt_user_info_comment where partner_id=?";
		$re=DB::select($sql,[$partner_id]);
		return isset($re[0]['count']) ? $re[0]['count'] : 0;
	}
	
	//指定用户的内容关键词
	function getKeyComment($user_id){
		$sql="select name from pt_keyword_conent where user_id = ? and status=1";
		$re=DB::select($sql,[$user_id]);
		if(isset($re[0]['name'])){
			$likeContent='';
			foreach($re as $k=>$v){
				if($k==0){
					$likeContent.="content like '%{$v['name']}%'";
				}else{
					$likeContent.=" or content like '%{$v['name']}%'";
				}
			}
			$likeContent="({$likeContent})";
		}
		return isset($likeContent) ? $likeContent : false;
	}
	
	
	
	
}